//
//  AddressViewController.m
//  FlowerRainGround
//
//  Created by lanou on 16/4/26.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "AddressViewController.h"
#import "WriterAddressViewController.h"
#import "addAddressManager.h"
#import "AddressTableViewCell.h"

#import "BuyGoodsViewController.h"
@interface AddressViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *addresstableView;
@property (nonatomic, strong) NSMutableArray *addressMrr;

@end

@implementation AddressViewController

- (NSMutableArray *)addressMrr{
    if (_addressMrr == nil) {
        self.addressMrr = [@[] mutableCopy];
    }
    return _addressMrr;
}


- (void) creatTableView{
    self.addresstableView.delegate = self;
    self.addresstableView.dataSource = self;
    self.addresstableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册
    [self.addresstableView registerNib:[UINib nibWithNibName:@"AddressTableViewCell" bundle:nil] forCellReuseIdentifier:@"address"];
    
    
    
    
    //设置新增地址的button
    UIButton *addaddressButton = [UIButton buttonWithType:UIButtonTypeSystem];
    addaddressButton.frame = CGRectMake(20, kHeight - 150, kWidth - 40, 60);
    addaddressButton.layer.borderWidth = 1;
    [addaddressButton setTitle:@"新增地址" forState:UIControlStateNormal];
    addaddressButton.titleLabel.textAlignment = 1;
    [addaddressButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    addaddressButton.block = ^(){
        [self addAddressVC];
    };
    addaddressButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:addaddressButton];
    
   
}


- (void) addAddressVC {
    WriterAddressViewController *write = [[WriterAddressViewController alloc] init];
    [self.navigationController pushViewController:write animated:YES];
}

//从数据库中获取数据
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    addAddressManager *manager = [[addAddressManager alloc] init];
    NSArray *array = [manager selectAll];
    
    self.addressMrr = [array mutableCopy];
    
    [self.addresstableView reloadData];//刷新数据
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"收货地址";
    self.navigationController.navigationBar.translucent = NO;
    [self creatTableView];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;//编辑
    

}



//行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.addressMrr.count;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"address"];
    addressModel *model = self.addressMrr[indexPath.row];
    if (!cell) {
        cell = [[AddressTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"address"];
    }
    
    cell.addressmodel = model;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    addressModel *model = self.addressMrr[indexPath.row];
    if (self.addressblock) {
        self.addressblock(model);
    }

    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark----------编辑-----------------
//编辑
- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.addresstableView setEditing:editing animated:animated];
}

//可以编辑的行
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}


//指定编辑样式
- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

//删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        addressModel *model = self.addressMrr[indexPath.row];
        //删除数据库中数据
        addAddressManager *manager = [[addAddressManager alloc] init];
        [manager cancelAddressModel:model];
        
        //删除数组
        [self.addressMrr removeObject:model];
        
        //删除行
        [self.addresstableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
